package com.uenpay.im.ui.old.activity.group;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.qyx.android.entity.GroupTalkEntity;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.weight.listview.RefreshListView;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-20 下午2:34:00
 * @copyRight http://www.qiyunxin.com
 * @function 搜索群
 */
public class SearchGroupsActivity extends BaseActivity implements
        RefreshListView.IRefreshListViewListener {

    private GroupMangerHandler mangerHandler = new GroupMangerHandler();
    private RefreshListView mListView;
    private SearchGroupResultAdapter groupResultAdapter;
    private String keyWord;
    private int page = 1;
    private EditText searchEt;
    private View loading;
    private TextView no_data_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_search_group_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
    }

    private void initView() {
        mListView = (RefreshListView) findViewById(R.id.discover_listview);
        searchEt = (EditText) findViewById(R.id.searchEt);
        loading = findViewById(R.id.loading);
        no_data_tv = (TextView) findViewById(R.id.no_data_tv);
    }

    private void initListener() {
        mListView.setPullLoadEnable(true);
        mListView.setRefreshListViewListener(this);
        findViewById(R.id.back_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        finish();
                    }
                });

        searchEt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    keyWord = searchEt.getText().toString();
                    page = 1;
                    loading.setVisibility(View.VISIBLE);
                    searchGroup();
                    Utils.hideSoftKeyboard(searchEt);
                    return true;
                }
                return false;
            }
        });

        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                GroupTalkEntity groupTalk = (GroupTalkEntity) arg0.getAdapter().getItem(
                        arg2);
                if (groupTalk != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("to_id", groupTalk.group_id + "");
                    ActivityHandle.startActivity(SearchGroupsActivity.this,
                            GroupDetailActivity.class, null, -1);
                }
            }
        });
    }

    private void searchGroup() {
        mangerHandler.searchGroupsByKeyWord(keyWord, page,
                new GroupMangerHandler.IGetMyGroupsListener() {

                    @Override
                    public void onGetMyGroupsResult(int status, String msg,
                                                    ArrayList<GroupTalkEntity> groups) {
                        onLoad();
                        loading.setVisibility(View.GONE);
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            if (page == 1) {
                                if (groups != null && groups.size() > 0) {
                                    if (groupResultAdapter == null) {
                                        groupResultAdapter = new SearchGroupResultAdapter(
                                                SearchGroupsActivity.this,
                                                groups);
                                    } else {
                                        groupResultAdapter.setData(groups);
                                    }
                                    mListView.setVisibility(View.VISIBLE);
                                    no_data_tv.setVisibility(View.GONE);
                                    mListView.setAdapter(groupResultAdapter);
                                } else {
                                    mListView.setVisibility(View.GONE);
                                    no_data_tv.setVisibility(View.VISIBLE);
                                }
                                mListView.setPullLoadEnable(true);
                            } else {
                                if (groups != null && groups.size() > 0) {
                                    groupResultAdapter.addData(groups);
                                } else {
                                    mListView.setPullLoadEnable(false);
                                }
                            }
                        }
                    }
                });
    }


    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
    }

    @Override
    public void onRefresh() {
        page = 1;
        searchGroup();
    }

    @Override
    public void onLoadMore() {
        page++;
        searchGroup();
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }
}
